package com.example.statemachine.service.impl;

import com.example.statemachine.entity.Credential;
import com.example.statemachine.machine.base.FsmStateService;
import com.example.statemachine.machine.enums.EnumFsmStatus;
import com.example.statemachine.util.BusinessException;
import org.springframework.stereotype.Service;

/**
 * @author jianwei.wang
 * @version v1.0
 * @date 2019/5/9 16:32
 **/
@Service("CredentialStateServiceImpl")
public class CredentialStateServiceImpl extends BaseServiceImpl<Credential> implements FsmStateService<Credential> {


    @Override
    public void tempStorage(Credential credential) {
        this.insertSelective(credential);
    }

    @Override
    public void submit(Credential credential) {
        credential.setStatus(EnumFsmStatus.IS_REVIEW.ordinal());
        int count = this.updateById(credential);
        if (count != 1){
            throw BusinessException.stateException();
        }
    }

    @Override
    public void withDraw(Credential credential) {
        credential.setStatus(EnumFsmStatus.TEMP_STORAGE.ordinal());
        int count = this.updateById(credential);
        if (count != 1){
            throw BusinessException.stateException();
        }
    }

    @Override
    public void review(Credential credential) {
        credential.setStatus(EnumFsmStatus.REVIEWED.ordinal());
        int count = this.updateById(credential);
        if (count != 1){
            throw BusinessException.stateException();
        }
    }

    @Override
    public void antiReview(Credential credential) {
        credential.setStatus(EnumFsmStatus.ANTI_REVIEWED.ordinal());
        int count = this.updateById(credential);
        if (count != 1){
            throw BusinessException.stateException();
        }
    }

    @Override
    public void cancel(Credential credential) {
        credential.setStatus(EnumFsmStatus.CANCELED.ordinal());
        int count = this.updateById(credential);
        if (count != 1){
            throw BusinessException.stateException();
        }
    }
}
